***********************************************
****** MERGING YIELDS - SPAIN AND FRANCE ******
***********************************************
/*
In this do file:
	
	FIRST: We merge Spanish and French yield data 
	on an quarterly basis.
	
	SECOND: We merge Spanish and French yield data 
	on an monthly basis.

*/

*----------------
* PATHS
*----------------
global replication `c(pwd)'
global data "$replication\Data"
global inter "$replication\Dta - Intermediate"
global final "$replication\Dta - Final"
global graphs "$replication\Graphs"

*==================
* QUARTERLY DATA
*==================
use "$inter\FR_DNSS_QuarterlyData.dta", clear
*global path C:

* Renaming variables
rename r* f_r*
rename beta* f_beta*

* Creating new variables
gen f_rbeta=f_beta0+f_beta1
gen f_sbeta=-f_beta1
replace f_rbeta=f_rbeta*100
replace f_sbeta=f_sbeta*100
label var f_rbeta "Short-term Rate Factor (%) France"
label var f_sbeta "Slope Factor (%)  France"

* Merging the quarterly French data with the Spanish data:
merge 1:1 qy using "$inter\ESP_DNSS_QuarterlyData.dta"
drop _merge

tsset qy
label var  qy "Quarter Year"

* Creating new variables
gen rbeta=beta0+beta1
gen sbeta=-beta1
replace rbeta=rbeta*100
replace sbeta=sbeta*100
label var rbeta "Short-term Rate Factor (%) Spain"
label var sbeta "Slope Factor (%) Spain"

foreach x of numlist 3 6 9 12 18 36 60 120 180 360 600{
	gen sp_`x' = f_r_`x'-r_`x'  
	lab var f_r_`x' "`x' Rate (France)"
	lab var r_`x' "`x' Rate (Spain)"
}

gen sp_sbeta=sbeta-f_sbeta
gen sp_rbeta=rbeta-f_rbeta
label var sp_rbeta "Short-term Rate Factor Spain/France Spread"
label var sp_sbeta  "Slope Factor Spain/France Spread"

save "$final\DNSS_QuarterlyData_FranceSpain.dta", replace

*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

*==================
* MONTHLY DATA
*==================
use "$inter\FR_DNSS_MonthlyData.dta", clear
*global path C:

* Renaming variables
rename r* f_r*
rename beta* f_beta*

* Generating variables
gen f_rbeta=f_beta0+f_beta1
gen f_sbeta=-f_beta1
replace f_rbeta=f_rbeta*100
replace f_sbeta=f_sbeta*100
label var f_rbeta "Short-term Rate Factor (%) France"
label var f_sbeta "Slope Factor (%)  France"

* Merging the monthly French data with the Spanish data:
merge 1:1 my using "$inter\ESP_DNSS_MonthlyData.dta"
drop _merge

* Generating new variables
gen rbeta=beta0+beta1
gen sbeta=-beta1
replace rbeta=rbeta*100
replace sbeta=sbeta*100
label var rbeta "Short-term Rate Factor (%) Spain"
label var sbeta "Slope Factor (%) Spain"

foreach x of numlist 3 6 9 12 18 36 60 120 180 360 600{
	gen sp_`x' = f_r_`x'-r_`x'  
	lab var f_r_`x' "`x' Month Rate (France)"
	lab var r_`x' "`x' Month Rate (Spain)"
}

gen sp_sbeta=sbeta-f_sbeta
gen sp_rbeta=rbeta-f_rbeta
label var sp_rbeta "Short-term Rate Factor Spain/France Spread"
label var sp_sbeta  "Slope Factor Spain/France Spread"

* GRAPH 1:
tsline sp_rbeta sp_sbeta, legend(pos(5) rows(1))
graph export "$graphs\Yields\factor_diffs.pdf", as(pdf) replace

tsset my

save "$final\DNSS_MonthlyData_FranceSpain.dta", replace
